﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.ConvertData
{
    class ConvertData
    {

        /**
     * \brief uint至string转换
     * \param[in]  input uint类型的ID值
     * \retval string 转换的16进制字符串
     */
        public static string HexToString(uint input)
        {
            return string.Format("0x{0:X8}", input);
        }

        /**
           * \brief 将字符串形式的数据转换为Uint32
           * 
           * \param[in] sourceStr 待转换的字符串列表信息
           * \param[in] type 待转换源字符串类型，主要用于报错
           * \param[ref] errorInfo 生成的错误信息
           * \param[in] allowEmpty 是否允许为空，true为空时填写NA，false不为空时必须填写序号（正整数）
           * \retval uint 倒计时值
           */
        public static uint StringToUint(string sourceStr, string type, ref string errorInfo, bool allowEmpty = true)
        {
            uint ret = 0;
            if (string.Empty == sourceStr)
            {
                errorInfo = string.Format("无效的数值{0},{1}不能不空", sourceStr, type);
            }
            else if (allowEmpty && sourceStr.Equals("NA"))
            {


            }
            else
            {
                try
                {
                    ret = Convert.ToUInt32(sourceStr);
                }
                catch
                {
                    ret = 0;
                    errorInfo = string.Format("{0}：无效的数值{1}", type, sourceStr);
                }
            }
            return ret;
        }

        /**
         * \brief 将字符串形式的数据转换为List<Uint32>
         * 
         * \param[in] sourceStr 待转换的字符串列表信息
         * \param[in] type 待转换源字符串类型，主要用于报错
         * \param[ref] errorInfo 生成的错误信息
         * \retval List<uint> 倒计时值
         */
        public static List<uint> StringToUintList(string sourceStr, string type, ref string errorInfo)
        {
            List<uint> ret = new List<uint>();
            if (string.Empty == sourceStr)
            {
                errorInfo = string.Format("无效的数值{0},{1}不能不空", sourceStr, type);
            }
            else if (sourceStr.Equals("NA"))
            {

            }
            else
            {
                foreach (var str in sourceStr.Split(','))
                {
                    if (string.Empty != str)
                    {
                        try
                        {
                            ret.Add(Convert.ToUInt32(str));
                        }
                        catch (Exception e)
                        {
                            errorInfo = string.Format("无效的数值{0},引发内部错误{1}", sourceStr, e.ToString());
                            break;
                        }
                    }
                }
            }
            return ret;
        }


        /**
        * \brief string至Uint32转换
        * 
        * \param[in]  idString 字符串类型的ID值
        * \param[ref]  valid 字符是否可用标识
        */
        public static uint StringToUint32(string idString, ref bool valid)
        {
            uint ret = uint.MaxValue;
            bool isHex = false;
            if (idString.StartsWith("0x") || idString.StartsWith("0X"))
            {
                isHex = true;
            }
            try
            {
                if (isHex)
                {
                    ret = Convert.ToUInt32(idString, 16);
                }
                else
                {
                    ret = Convert.ToUInt32(idString);
                }
            }
            catch (Exception)
            {
                valid = false;
            }
            return ret;
        }
        /**
         * \brief string至Uint16转换
         * 
         * \param[in]  idString 字符串类型的ID值
         * \param[ref]  valid 字符是否可用标识
         */
        public static UInt16 StringToUint16(string idString, ref bool valid)
        {
            UInt16 ret = UInt16.MaxValue;
            bool isHex = false;
            if (idString.StartsWith("0x") || idString.StartsWith("0X"))
            {
                isHex = true;
            }
            try
            {
                if (isHex)
                {
                    ret = Convert.ToUInt16(idString, 16);
                }
                else
                {
                    ret = Convert.ToUInt16(idString);
                }
            }
            catch (Exception)
            {
                valid = false;
            }
            return ret;
        }
        /**
         * \brief string至byte转换
         * 
         * \param[in]  idString 字符串类型的ID值
         * \param[ref]  valid 字符是否可用标识
         */
        public static byte StringToByte(string idString, ref bool valid)
        {
            byte ret = byte.MaxValue;
            bool isHex = false;
            if (idString.StartsWith("0x") || idString.StartsWith("0X"))
            {
                isHex = true;
            }
            try
            {
                if (isHex)
                {
                    ret = Convert.ToByte(idString, 16);
                }
                else
                {
                    ret = Convert.ToByte(idString);
                }
            }
            catch (Exception)
            {
                valid = false;
            }
            return ret;
        }
        /**
         * \brief string至bool类型转换
         * 
         * \param[in]  idString 字符串类型的ID值
         * \param[ref]  valid 字符是否可用标识
         */
        public static bool StringToBool(string str, ref bool valid)
        {
            bool ret = false;
            switch (str)
            {
                case "是":
                    ret = true;
                    break;
                case "否":
                    ret = false;
                    break;
                default:
                    ret = false;
                    valid = false;
                    break;
            }
            return ret;
        }

        /**
           * \brief 检测字符串是否转为bool类型，捕获并返回错误数据
           * \param[in] namestring 待检测的string值结果
           * \param[in] type 标识字符串0
           * \param[in] name 标识字符串1
           * \param[in] markstring 标识字符串2
           * \param[in] addError 添加错误信息委托实例
           */
        public static bool StringIsBoolEvaluate(bool isrequired, string namestring, string type, string name, string markstring,
            Action<string, string, string> addError)  //2019.05.23 wj 新增
        {
            bool valid = true;
            if (!string.IsNullOrEmpty(namestring))
            {
                bool strbool = StringToBool(namestring, ref valid);
                if (!valid)
                {
                    addError(type, name, "无效的" + markstring);
                }
                return strbool;
            }
            else
            {
                if (isrequired)
                {
                    addError(type, name, markstring + "参数未填写，请补充！");
                }
                return false;
            }
        }

        /**
           * \brief 检测字符串是否为空，捕获并返回错误数据
           * \param[in] namestring 待检测的string值结果
           * \param[in] type 标识字符串0
           * \param[in] name 标识字符串1
           * \param[in] markstring 标识字符串2
           * \param[in] addError 添加错误信息委托实例
           */
        public static bool StringIsNullOrEmptyEvaluate(string namestring, string type, string name, string markstring,
            Action<string, string, string> addError)    //2019.05.23 wj 新增
        {
            bool ret = false;
            if (string.IsNullOrEmpty(namestring))
            {
                addError(type, name, markstring + "参数未填写，请补充！");
                ret = true;
            }
            return ret;
        }

        /**
           * \brief 检测字符串是否符合规范，参数为NA、Auto或其他，捕获并返回错误数据
           * \param[in] namestring 待检测的string值结果
           * \param[in] type 标识字符串0
           * \param[in] name 标识字符串1
           * \param[in] markstring 标识字符串2
           * \param[in] addError 添加错误信息委托实例
           */
        public static void StringIsNAEvaluate(ref string namestring, string type, string name, string markstring,
            Action<string, string, string> addError)    //2019.05.31 wj 新增
        {
            switch (namestring)
            {
                case "NA":
                    namestring = String.Empty;
                    break;
                case "Auto":
                    namestring = "Auto";
                    break;
                default:
                    addError(type, name, "无效的" + markstring + namestring);
                    break;
            }
        }

    }
}
